<?php

namespace App\Admin\Controllers;

use App\Models\Province;
use App\Models\University;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class UniversityController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('大学');
            $content->description('大学列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(University::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                // 校区按钮
                $actions->prepend('<a href="'.url("/admin/campus?university_id=". $actions->getKey()).'">校区</a>');
            });
            $grid->id('ID')->sortable();
            $grid->name('大学名称');
            $grid->province('省份');
            $grid->campus('大学校区')->display(function ($data) {
                $text='';
                foreach($data as $v){
                    $text.='('.$v['name'].')';
                }
                return $text;
            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(University::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('name','大学名称');
            $provinces=Province::orderBy('sort','desc')->get();
            foreach($provinces as $v){
                $options[$v->name]=$v->name;
            }
            $form->select('province','省份')->options($options);


        });
    }
}
